package com.pj.wisdomlogistics.ui.view.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.zhy.autolayout.utils.AutoUtils;

/**
 * Created by zhang on 2016/9/1.
 */
public abstract class AbsAdapter<VH extends BaseViewHolder> extends RecyclerView.Adapter<BaseViewHolder>{

    public static final int VIEW_TYPE_HEADER = -1;
    public static final int VIEW_TYPE_FOOTER = -2;
    private static final String TAG = "AbsAdapter";

    protected View headerView;
    protected View footerView;

    protected Context context;

    public AbsAdapter(Context context) {
        this.context = context;
    }

    public abstract VH createCustomViewHolder(ViewGroup parent,int viewType);
    public abstract void bindCustomViewHolder(VH viewHolder,int position);

    @Override
    public BaseViewHolder  onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType==VIEW_TYPE_HEADER){
            return new BaseViewHolder(headerView);
        }
        else if (viewType==VIEW_TYPE_FOOTER){
            return  new BaseViewHolder(footerView);
        }
        else{
            return createCustomViewHolder(parent,viewType);
        }

    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        AutoUtils.autoSize(holder.itemView);
        switch (holder.getItemViewType()) {
            case VIEW_TYPE_HEADER:
            case VIEW_TYPE_FOOTER:
                break;
            default:
                bindCustomViewHolder((VH) holder, position);
                break;
        }
    }

    /**
     * 添加HeaderView
     *
     * @param headerView 顶部View对象
     */
    public void addHeaderView(View headerView) {
        if (headerView == null) {
            Log.w(TAG, "add the header view is null");
            return;
        }
        this.headerView = headerView;
        notifyDataSetChanged();
    }

    /**
     * 移除HeaderView
     */
    public void removeHeaderView() {
        if (headerView != null) {
            headerView = null;
            notifyDataSetChanged();
        }
    }

    /**
     * 添加FooterView
     *
     * @param footerView View对象
     */
    public void addFooterView(View footerView) {
        if (footerView == null) {
            Log.w(TAG, "add the footer view is null");
            return;
        }
        this.footerView = footerView;
        notifyDataSetChanged();
    }

    /**
     * 移除FooterView
     */
    public void removeFooterView() {
        if (footerView != null) {
            footerView = null;
            notifyDataSetChanged();
        }
    }

    public int getHeaderCount() {
        return headerView==null?0:1;
    }

    public int getFooterCount() {
        return footerView==null?0:1;
    }

    /**
     * 获取附加HeaderView和FooterView的数量
     * @return
     */
    public int getExtraCount() {
        return getFooterCount()+getHeaderCount();
    }

    @Override
    public abstract long getItemId(int position) ;
}
